/*
 * @Author: 0x9DEFA478
 * @Date: 2024-08-04 15:13:35
 * @LastEditTime: 2024-08-06 21:24:21
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "./H_TS_Queue.h"
#include "../LL/H_TS_LL.h"




static void* Pop(H_TS_Queue* Queue){
  void* Item;
  Hsize ReadIndex;

  H_TS_Semaphore_AcquireLock(&Queue->Semaphore);

  ReadIndex=Queue->ReadIndex;

  if(ReadIndex!=Queue->WriteIndex){
    Item=Queue->Data[ReadIndex];
  }else{
    Item=NULL;
    goto lReturn;
  }

  if(Item==NULL){
    goto lReturn;
  }

  Queue->Data[ReadIndex]=NULL;

  ReadIndex++;
  if(ReadIndex>=Queue->Size){
    ReadIndex=0;
  }
  Queue->ReadIndex=ReadIndex;

lReturn:
  H_TS_Semaphore_AcquireUnlock(&Queue->Semaphore);
  return Item;
}




int H_TS_Queue_TryPush_ISR(H_TS_Queue* Queue,void* Item){
  Hsize newWriteIndex;

  oH_TS_Assert_HandleValid(Queue==NULL,"H_TS:H_TS_Queue_TryPush_ISR():空的句柄");
  oH_TS_Assert_HandleValid(Item==NULL,"H_TS:H_TS_Queue_TryPush_ISR():空的Item");

  newWriteIndex=H_TS_LL_AtomicAccess_WriteIndexIncrement(&Queue->WriteIndex,&Queue->ReadIndex,Queue->Size);

  if(newWriteIndex<0){
    return -1;
  }

  Queue->Data[newWriteIndex]=Item;
  H_TS_Semaphore_TryRelease_ISR(&Queue->Semaphore);

  return 0;
}

int H_TS_Queue_TryPush(H_TS_Queue* Queue,void* Item){
  Hsize newWriteIndex;

  oH_TS_Assert_HandleValid(Queue==NULL,"H_TS:H_TS_Queue_TryPush():空的句柄");
  oH_TS_Assert_HandleValid(Item==NULL,"H_TS:H_TS_Queue_TryPush():空的Item");

  if((newWriteIndex=H_TS_LL_AtomicAccess_WriteIndexIncrement(&Queue->WriteIndex,&Queue->ReadIndex,Queue->Size))<0){
    return -1;
  }
  
  Queue->Data[newWriteIndex]=Item;
  H_TS_Semaphore_TryRelease(&Queue->Semaphore);

  return 0;
}

void H_TS_Queue_Push(H_TS_Queue* Queue,void* Item){
  Hsize newWriteIndex;

  oH_TS_Assert_HandleValid(Queue==NULL,"H_TS:H_TS_Queue_Push():空的句柄");
  oH_TS_Assert_HandleValid(Item==NULL,"H_TS:H_TS_Queue_Push():空的Item");

  while((newWriteIndex=H_TS_LL_AtomicAccess_WriteIndexIncrement(&Queue->WriteIndex,&Queue->ReadIndex,Queue->Size))<0){
    H_TS_Semaphore_Release(&Queue->Semaphore);
  }
  
  Queue->Data[newWriteIndex]=Item;
  H_TS_Semaphore_TryRelease(&Queue->Semaphore);
}

int H_TS_Queue_Push_Time(H_TS_Queue* Queue,void* Item,H_TS_Tick Timeout){
  Hsize newWriteIndex;
  H_TS_Tick DeadLine;

  oH_TS_Assert_HandleValid(Queue==NULL,"H_TS:H_TS_Queue_Push_Time():空的句柄");
  oH_TS_Assert_HandleValid(Item==NULL,"H_TS:H_TS_Queue_Push_Time():空的Item");
  oH_TS_Assert_ParamValid(Timeout<0,"H_TS:H_TS_Queue_Push_Time():Timeout小于0");

  if(Timeout<=0){
    return H_TS_Queue_TryPush(Queue,Item);
  }

  DeadLine=H_TS_LL_GetRunningTick()+Timeout;
  while((newWriteIndex=H_TS_LL_AtomicAccess_WriteIndexIncrement(&Queue->WriteIndex,&Queue->ReadIndex,Queue->Size))<0){
    H_TS_Tick timeout;
    timeout=DeadLine-H_TS_LL_GetRunningTick();
    if(timeout<=0){
      break;
    }
    H_TS_Semaphore_Release_Time(&Queue->Semaphore,timeout);
  }

  if(newWriteIndex<0){
    return -1;
  }
  
  Queue->Data[newWriteIndex]=Item;
  H_TS_Semaphore_TryRelease(&Queue->Semaphore);

  return 0;
}

void* H_TS_Queue_TryPop(H_TS_Queue* Queue){
  void* Item;
  
  oH_TS_Assert_HandleValid(Queue==NULL,"H_TS:H_TS_Queue_TryPop():空的句柄");

  if((Item=Pop(Queue))==NULL){
    return Item;
  }

  H_TS_Semaphore_TryAcquire(&Queue->Semaphore);
  return Item;
}

void* H_TS_Queue_Pop(H_TS_Queue* Queue){
  void* Item;
  
  oH_TS_Assert_HandleValid(Queue==NULL,"H_TS:H_TS_Queue_Pop():空的句柄");

  while((Item=Pop(Queue))==NULL){
    H_TS_Semaphore_Acquire(&Queue->Semaphore);
  }

  H_TS_Semaphore_TryAcquire(&Queue->Semaphore);
  return Item;
}

void* H_TS_Queue_Pop_Time(H_TS_Queue* Queue,H_TS_Tick Timeout){
  void* Item;
  H_TS_Tick DeadLine;

  oH_TS_Assert_HandleValid(Queue==NULL,"H_TS:H_TS_Queue_Pop():空的句柄");
  oH_TS_Assert_ParamValid(Timeout<0,"H_TS:H_TS_Queue_Push_Time():Timeout小于0");

  if(Timeout<=0){
    return H_TS_Queue_TryPop(Queue);
  }

  DeadLine=H_TS_LL_GetRunningTick()+Timeout;
  while((Item=Pop(Queue))==NULL){
    H_TS_Tick timeout;
    timeout=DeadLine-H_TS_LL_GetRunningTick();
    if(timeout<=0){
      break;
    }
    H_TS_Semaphore_Acquire_Time(&Queue->Semaphore,timeout);
  }

  H_TS_Semaphore_TryAcquire(&Queue->Semaphore);
  return Item;
}



